package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.service.ISysHeatFeeTemplateService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysHousing;
import com.ruoyi.system.service.ISysHousingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 小区Controller
 * 
 * @author ruoyi
 * @date 2024-08-07
 */
@RestController
@RequestMapping("/system/housing")
public class SysHousingController extends BaseController
{
    @Autowired
    private ISysHousingService sysHousingService;

    @Autowired
    private ISysHeatFeeTemplateService sysHeatFeeTemplateService;
    /**
     * 查询小区列表
     */
    @PreAuthorize("@ss.hasPermi('housing:housing:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysHousing sysHousing)
    {
        startPage();
        sysHousing.setDeptId(getDeptId());
        List<SysHousing> list = sysHousingService.selectSysHousingList(sysHousing);
        return getDataTable(list);
    }

    /**
     * 查询小区列表
     */
    @PreAuthorize("@ss.hasPermi('housing:housing:list')")
    @GetMapping("/allList")
    public AjaxResult allList(SysHousing sysHousing)
    {
        sysHousing.setDeptId(getDeptId());
        List<SysHousing> list = sysHousingService.selectSysHousingList(sysHousing);
        return success(list);
    }

    /**
     * 导出小区列表
     */
    @PreAuthorize("@ss.hasPermi('housing:housing:export')")
    @Log(title = "小区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysHousing sysHousing)
    {
        sysHousing.setDeptId(getDeptId());
        List<SysHousing> list = sysHousingService.selectSysHousingList(sysHousing);
        ExcelUtil<SysHousing> util = new ExcelUtil<SysHousing>(SysHousing.class);
        util.exportExcel(response, list, "小区数据");
    }

    /**
     * 获取小区详细信息
     */
    @PreAuthorize("@ss.hasPermi('housing:housing:query')")
    @GetMapping(value = "/{housingId}")
    public AjaxResult getInfo(@PathVariable("housingId") Long housingId)
    {
        return success(sysHousingService.selectSysHousingByHousingId(housingId));
    }

    /**
     * 新增小区
     */
    @PreAuthorize("@ss.hasPermi('housing:housing:add')")
    @Log(title = "小区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysHousing sysHousing)
    {
        sysHousing.setDeptId(getDeptId());
        return toAjax(sysHousingService.insertSysHousing(sysHousing));
    }

    /**
     * 修改小区
     */
    @PreAuthorize("@ss.hasPermi('housing:housing:edit')")
    @Log(title = "小区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysHousing sysHousing)
    {
        return toAjax(sysHousingService.updateSysHousing(sysHousing));
    }

    /**
     * 修改热费计算模板
     */
    @PreAuthorize("@ss.hasPermi('system:template:bind')")
    @Log(title = "绑定热费", businessType = BusinessType.UPDATE)
    @PostMapping("/bind/heat")
    public AjaxResult bindTemplate(@RequestBody SysHousing sysHousing)
    {
        sysHeatFeeTemplateService.bindHeatFeeTemplate(sysHousing.getHeatFeeId(),sysHousing.getHousingId());
        return success();
    }
    /**
     * 删除小区
     */
    @PreAuthorize("@ss.hasPermi('housing:housing:remove')")
    @Log(title = "小区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{housingIds}")
    public AjaxResult remove(@PathVariable Long[] housingIds)
    {
        return toAjax(sysHousingService.deleteSysHousingByHousingIds(housingIds));
    }
}
